/**
 **  表单验证模块
 **/
function doValidate(formId) {
	if(!formId){
		formId = 'frmSubmit';
	}
	if($('#'+formId).html()==undefined) {
		alert('Please make sure the validate form named '+formId+' exist!!');
	} else {
		
		$.mask.definitions['~']='[+-]';
		$('#phone').mask('(999) 999-999-9999');
		$('.emsTimePicker').mask('9999-99-99 99:99:99');
		//$('.ipAddress').mask('999.999.999.999');
	
		jQuery.validator.addMethod("phone", function (value, element) {
			return this.optional(element) || /^\(\d{3}\) -\d{3}\-\d{3}\-\d{4}( x\d{1,6})?$/.test(value);
		}, "Enter a valid phone number.");
		
		jQuery.validator.addMethod("emsTimePicker", function (value, element) {
			var reg=/^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/;
			return this.optional(element) || reg.test(value);
		}, "");
		
		jQuery.validator.addMethod("ipAddress", function (value, element) {
			return this.optional(element) || /^(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])$/.test(value);
		}, "Enter a valid ip.");
		
		
		$('#'+formId).validate({
			errorElement: 'div',
			errorClass: 'help-block',
			focusInvalid: false,
			rules: {
				email: {
					required: true,
					email:true
				},
				password: {
					required: true,
					minlength: 5
				},
				password2: {
					required: true,
					minlength: 5,
					equalTo: "#password"
				},
				name: {
					required: true
				},
				phone: {
					required: true,
					phone: 'required'
				},
				url: {
					required: true,
					url: true
				},
				comment: {
					required: true
				},
				state: {
					required: true
				},
				platform: {
					required: true
				},
				subscription: {
					required: true
				},
				ipAddress:{
					required: true
				},
				emsTimePicker:{
					
				},
				gender: 'required',
				agree: 'required'
			},

			messages: {
				email: {
					required: "请填入有效的email地址.",
					email: "请提供有效的email地址."
				},
				password: {
					required: "Please specify a password.",
					minlength: "Please specify a secure password."
				},
				subscription: "Please choose at least one option",
				gender: "Please choose gender",
				agree: "Please accept our policy"
			},

			invalidHandler: function (event, validator) { //display error alert on form submit   
				$('.alert-danger', $('.login-form')).show();
			},

			highlight: function (e) {
				$(e).closest('.form-group').removeClass('has-info').addClass('has-error');
			},

			success: function (e) {
				$(e).closest('.form-group').removeClass('has-error').addClass('has-info');
				$(e).remove();
			},

			errorPlacement: function (error, element) {
				if(element.is(':checkbox') || element.is(':radio')) {
					var controls = element.closest('div[class*="col-"]');
					if(controls.find(':checkbox,:radio').length > 1) controls.append(error);
					else error.insertAfter(element.nextAll('.lbl:eq(0)').eq(0));
				}
				else if(element.is('.select2')) {
					error.insertAfter(element.siblings('[class*="select2-container"]:eq(0)'));
				}
				else if(element.is('.chosen-select')) {
					error.insertAfter(element.siblings('[class*="chosen-container"]:eq(0)'));
				}
				else error.insertAfter(element.parent());
			},

			submitHandler: function (form) {
			},
			invalidHandler: function (form) {
			}
		});
	}
}
